<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 17-11-6
 * Time: 上午10:48
 */

namespace common\components\gps;



use common\components\tools\Tools;
use linslin\yii2\curl\Curl;
use yii\httpclient\Client;

class Exlive extends Gps implements Api
{
    const PRE_SIGN = '145';
    const NAME = 'exlive';
    const NOTE = '茗鸿'; // http://www.exlive.cn/page/pageAction_getIndexPage.action?type=0
    const VERSION = 1;

    public function addGps()
    {
        return [
            'other_data' => [
                'success' => true,
                'msg' => '',
            ],
        ];
    }

    /**
     * 车辆最新位置 http://60.195.248.67:89/gpsonline/GPSAPI?version=1&method=loadLocation&vid=4103747&vKey=2e8b868d779f1e1c0696e04113e79003
     * @return array
     */
    public function getGps()
    {
        $this->checkOrder();
        $order = $this->order;
        $name = $order->gps_serial_number;
        $t = $this->carLogin($name);
        $t = Tools::objectToArray($t);
        $_config = ['baseUrl' => Config::$exlive['get_gps_uri']];
        $__data = [
            'version' => self::VERSION,
            'method' => 'loadLocation',
            'vid' => $t['other_data']['vid'],
            'vKey' => $t['other_data']['vKey'],
        ];
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('get')
            ->setUrl($_config['baseUrl'])
            ->setData($__data)
            ->send();
        $other_data = [];
        if (isset($response->content)){
            $x = json_decode($response->content);
            $x = Tools::objectToArray($x);
            $other_data['beautiful_back'] = $x;
            if (abs($x['locs'][0]['gpstime']/1000-time())<1800){
                $other_data['gps_status'] = '已上线';
                $other_data['gps_online_time'] = $x['locs'][0]['gpstime']/1000;
            }else{
                $other_data['gps_status'] = '未上线';
            }
        }else{
            $other_data['gps_status'] = '未上线';
        }
        return [
            'platform_back' => $response->content,
            'other_data' => $other_data,
        ];
    }

    /**
     * 车辆地图位置接口 http://60.195.248.67:89/Interface/findPosition.action?carNum=京G41064天顺&u=gbk
     */
    public function getGpsLocationData()
    {
        $data = [];
        $this->checkOrder();
        $order = $this->order;
        $t = $this->getGps();
        if (isset($t['other_data']['beautiful_back']['locs'][0]['name'])){
            /*$client = new Client();
            $response = $client->createRequest()
                ->setMethod('get')
                ->setUrl(Config::$exlive['get_map_url'])
//            ->setData(['carNum'=>$order->gps_serial_number])
//            ->setData(['carNum'=>'D葛俊山9457'])
                ->setData(['carNum'=>$t['other_data']['beautiful_back']['locs'][0]['name']])
                ->send();
            var_dump($response);
            var_dump($response->content);*/
            $data['url'] = Config::$exlive['get_map_url'].'?carNum='.$t['other_data']['beautiful_back']['locs'][0]['name'];
            $data['html'] = '<script type="text/javascript">location.href = "'.$data['url'].'";</script>';
        }
        return $data;
    }

    /**
     * 用户登录 http://60.195.248.67:89/gpsonline/GPSAPI?version=1&method=loginSystem&name=sunyan&pwd=123456
     */
    public function userLogin()
    {
        $_config = ['baseUrl' => Config::$exlive['get_gps_uri']];
        $__data = [
            'version' => self::VERSION,
            'method' => 'loginSystem',
            'name' => Config::$exlive['user'],
            'pwd' => Config::$exlive['password'],
        ];
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('get')
            ->setUrl($_config['baseUrl'])
            ->setData($__data)
            ->send();
    }

    /**
     * 车辆登录 http://60.195.248.67:89/gpsonline/GPSAPI?version=1&method=vLoginSystem&name=辽HB929&pwd=000000
     * @return array
     */
    public function carLogin($name)
    {
        $_config = ['baseUrl' => Config::$exlive['get_gps_uri']];
        $__data = [
            'version' => self::VERSION,
            'method' => 'vLoginSystem',
            'name' => $name,
            'pwd' => Config::$exlive['car_pwd'],
        ];
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('post')
            ->setUrl($_config['baseUrl'])
            ->addHeaders(['content-type' => 'application/x-www-form-urlencoded'])
            ->setData($__data)
            ->send();
//        $curl = new Curl();
//        $r = $curl->setPostParams($__data)
//            ->post($_config['baseUrl']);
        return [
            '__post' => $__data,
            'plat_back' => $response->content,
            'other_data' => json_decode($response->content),
//            'r' => $r,
        ];
    }
}